package com.cq.controller;

import com.cq.pojo.Meeting;
import com.cq.pojo.Result;
import com.cq.pojo.Topic;
import com.cq.service.ReplyService;
import com.cq.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: 悠久的苍穹
 * @Date: 2021/2/2 14:48
 * @Description: 话题控制层
 * @Version: 1.0
 */
@Controller
public class TopicController {
    @Autowired
    TopicService topicService;
    @Autowired
    ReplyService replyService;

    @ResponseBody
    @PostMapping("/createTopic")
    public Result createTopic(@RequestBody Map<String,Object> map) throws Exception {
        Result result = new Result();
        String nickname = map.get("nickname").toString();
        String avatarUrl = map.get("avatarUrl").toString();
        String content = map.get("content").toString();
        String img = map.get("img").toString();
        Topic topic = new Topic();
        topic.setNickname(nickname);
        topic.setAvatarUrl(avatarUrl);
        topic.setContent(content);
        topic.setImg(img);
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        topic.setCreatetime(dateFormat.format(date));
        topic.setIshot(0);
        int i = topicService.addTopic(topic);
        if(i==1){
            result.setCode(200);
            result.setMsg("创建成功");
        }else{
            result.setCode(500);
            result.setMsg("创建失败");
        }
        return result;
    }

    @ResponseBody
    @GetMapping("/getTopics")
    public Result getTopics(@RequestParam int page,
                            @RequestParam int limit) {
        Result result = new Result();
        int count = topicService.getCount();
        List<Topic> topicList = topicService.getTopics(page,limit);
        result.setCode(0);
        result.setCount(count);
        result.setData(topicList);
        result.setMsg("查询成功");
        return result;
    }

    @GetMapping("/getTopicsByDate")
    @ResponseBody
    public Result getTopicsByDate( @RequestParam int page,
                                    @RequestParam int limit,
                                    @RequestParam String start,
                                    @RequestParam String end) {
        Result result = new Result();
        int count = topicService.getTopicCount(start, end);
        List<Topic> topicList = topicService.getTopicsByTime(page, limit, start, end);
        result.setCount(count);
        result.setData(topicList);
        result.setCode(0);
        return result;
    }

    @GetMapping("getTopicAndReplyByWx")
    @ResponseBody
    public Result getTopicAndReplyByWx() {
        Result result = new Result();
        List<Topic> topicList = topicService.getTopicAndReply();
        result.setCode(200);
        result.setData(topicList);
        return result;
    }

    @DeleteMapping("/delTopic/{id}")
    @ResponseBody
    public Result delTopic(@PathVariable("id") String id) {
        Result result = new Result();
        String[] s = id.split(",");
        for (int i = 0; i < s.length; i++) {
            topicService.deleteTopicById(Integer.parseInt(s[i]));
            replyService.deleteReplyByTopicId(Integer.parseInt(s[i]));
        }
        result.setCode(200);
        return result;
    }

    @GetMapping("/editTopicUI/{id}")
    public String editTopicUI(@PathVariable("id") int id, Model model){
        Topic topic = topicService.getTopicById(id);
        model.addAttribute("topic",topic);
        return "app/topic/topic/listform";
    }

    @ResponseBody
    @GetMapping("/updateTopicHot/{id}")
    public Result updateTopicHot(@PathVariable("id") String id){
        Result result = new Result();
        topicService.updateTopicHot(Integer.parseInt(id));
        result.setCode(200);
        result.setMsg("设置热门成功");
        return result;
    }
    @ResponseBody
    @GetMapping("/updateTopicCold/{id}")
    public Result updateTopicCold(@PathVariable("id") String id){
        Result result = new Result();
        topicService.updateTopicCold(Integer.parseInt(id));
        result.setCode(200);
        result.setMsg("设置冷门成功");
        return result;
    }


    @ResponseBody
    @GetMapping("/getHotTopicsByWX")
    public Result getHotTopicsByWX(){
        Result result = new Result();
        List<Topic> topicList = topicService.getHotTopics();
        result.setCode(200);
        result.setData(topicList);
        result.setMsg("查询成功");
        return result;
    }

    @ResponseBody
    @GetMapping("/getTopicAndReplyByIdByWx/{id}")
    public Result getTopicAndReplyByIdByWx(@PathVariable("id") String id){
        Result result = new Result();
        Topic topic = topicService.getTopicAndReplyById(Integer.parseInt(id));
        List<Topic> topicList = new ArrayList<>();
        topicList.add(topic);
        result.setCode(200);
        result.setData(topicList);
        result.setMsg("查询成功");
        return result;
    }


    @PostMapping("getTopicAndReplyByCreate")
    @ResponseBody
    public Result getTopicAndReplyByCreate(@RequestBody Map<String,Object> map) {
        Result result = new Result();
        String nickName = map.get("nickname").toString();
        List<Topic> topicList = topicService.getTopicAndReplyByCreate(nickName);
        result.setCode(200);
        result.setData(topicList);
        return result;
    }
    
}
